package com.actor.tinker;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import androidx.annotation.Keep;

import com.tencent.tinker.loader.shareutil.ShareConstants;
import com.tencent.tinker.loader.shareutil.ShareTinkerInternals;

/**
 * description: 额外写的配置文件
 * company    :
 *
 * @author : ldf
 * date       : 2024/10/10 on 11
 * @version 1.0
 */
@Keep
@com.tencent.tinker.anno.Keep
public class TinkerConfig {

    /**
     * if添加的有以下依赖, 就会自动生成在 SampleApplicationLike 上标注的 Application
     * annotationProcessor('com.tencent.tinker:tinker-android-anno:${TINKER_VERSION}')
     * 所以, 最好不要添加 ↑ 依赖, 否则自己的 MyApplication 不能用, 初始化工作也只能挪到 SampleApplicationLike 中去..
     */

    /**
     * <a href="https://github.com/Tencent/tinker/issues/795">让用户完全使用自己的Application类 · Issue #795 · Tencent/tinker</a> <br />
     * 自己已有的 MyApplication 里的初始化无效, 原因是会被 Tinker 自动生成的Application替换, 导致不走 MyApplication 的onCreate()等方法??
     */
//    private static final String APPLICATION_NAME = "com.actor.testapplication.MyApplication";
    /**
     * 编译后, 系统会在这个目录
     * Project\app\build\generated\ap_generated_sources\debug\out\tinker\sample\android\app
     * 下生成 SampleApplication, 代码如下:
     * <pre>
     * package tinker.sample.android.app;
     * import com.tencent.tinker.loader.app.TinkerApplication;
     *
     * public class SampleApplication extends TinkerApplication {
     *     public SampleApplication() {
     *         //下方的参数, 就是你 SampleApplicationLike 注解上的参数!
     *         super(15, "com.tencent.tinker.loader.SampleApplicationLike", "com.tencent.tinker.loader.TinkerLoader_1", false, true, false);
     *     }
     * }
     * </pre>
     */
    private static final String APPLICATION_NAME = "tinker.sample.android.app.SampleApplication";

    public static final String LOADER_CLASS_NAME = "com.tencent.tinker.loader.TinkerLoader_1";

    public static String PLATFORM = "all";


    /**
     * 设置Tinker是否可以更新patch, 更新补丁包前可以设置(比如在Application, Activity等)
     * {@link ShareTinkerInternals#setTinkerDisableWithSharedPreferences(Context)} <br />
     * ∵在 {@link com.actor.tinker.crash.SampleUncaughtExceptionHandler#tinkerPreVerifiedCrashHandler(Throwable)}
     * 捕捉到异常后, 会在sp设置设置为false, 导致如果不清缓存的话, 就一直不能更新patch,
     * ∴这儿增加了这个方法
     * @param isEnable 是否可以更新patch
     */
    public static void setTinkerEnableWithSharedPreferences(Context context, boolean isEnable) {
//        ShareTinkerInternals.setTinkerDisableWithSharedPreferences(applicationLike.getApplication());
        SharedPreferences sp = context.getSharedPreferences(ShareConstants.TINKER_SHARE_PREFERENCE_CONFIG, Context.MODE_MULTI_PROCESS);
        String keyName = getTinkerSwitchSPKey(context);
        sp.edit().putBoolean(keyName, isEnable).commit();
    }

    /**
     * {@link ShareTinkerInternals#getTinkerSwitchSPKey(Context)}
     * @return "tinker_enable_" + "1.9.14.27" + "_" + tinkerId
     */
    public static String getTinkerSwitchSPKey(Context context) {
        return ShareConstants.TINKER_ENABLE_CONFIG_PREFIX + ShareConstants.TINKER_VERSION + "_" + getTinkerId(context);
    }


    /**
     * 返回tinkerId, 见 gradle.properties 你自己的配置
     * {@link ShareTinkerInternals#getTinkerSwitchSPKey(Context)}
     * @return "tinker_id_" + versionCode + "_" + patchCount
     */
    public static String getTinkerId(Context context) {
        String tmpTinkerId = ShareTinkerInternals.getManifestTinkerID(context);
        if (TextUtils.isEmpty(tmpTinkerId)) {
            tmpTinkerId = "@@";
        }
        return tmpTinkerId;
    }
}
